<template>
  <div class="app-container">
    <a-title @clickPreview="preview" />
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="viewList"
        />
      </el-form-item>
      <el-form-item label="重要程度" prop="importanceDegree">
        <el-select
          v-model="queryParams.importanceDegree"
          placeholder="重要程度"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.value"
            :label="dict.text"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="viewList"
          v-hasPermi="['project:stakeholder:list']"
        >查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-if="isvalid"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-if="isvalid"
        >删除</el-button>
      </el-col>
    </el-row>
    <div class="tablebox">
      <el-table
        stripe
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        ref="multipleTable"
        @selection-change="handleSelectionChange"
        @sort-change="tableSortChange"
        :default-sort="{prop: 'importanceDegree', order: 'descending'}"
      >
        <el-table-column
          type="selection"
          width="55"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        ></el-table-column>
        <el-table-column
          label="姓名"
          prop="name"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        ></el-table-column>
        <el-table-column
          label="身份"
          prop="identity"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        ></el-table-column>
        <el-table-column
          label="重要程度"
          width="150"
          prop="importanceDegree"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        >
          <template slot-scope="scope">
            <el-rate v-model="scope.row.importanceDegree" disabled></el-rate>
          </template>
        </el-table-column>
        <el-table-column
          label="单位"
          prop="unit"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        ></el-table-column>
        <el-table-column
          label="部门"
          prop="dept"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        ></el-table-column>
        <el-table-column
          label="岗位"
          prop="post"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        ></el-table-column>
        <el-table-column
          label="电话"
          prop="phonenumber"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        ></el-table-column>
        <el-table-column
          label="邮箱"
          prop="email"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        ></el-table-column>
        <el-table-column
          label="备注"
          prop="remark1"
          :show-overflow-tooltip="true"
          :sort-orders="['ascending', 'descending']"
          sortable="custom"
        ></el-table-column>
        <el-table-column width="170" label="操作">
          <template slot-scope="scope">
            <a @click="handleEdit(scope.$index, scope.row)" v-if="isvalid">
              <i class="el-icon-edit aicon"></i>编辑
            </a>
            <a @click="handleView(scope.$index, scope.row)" v-if="isvalid">
              <i class="el-icon-price-tag aicon"></i>
              便签
            </a>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="viewList"
      />
    </div>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="60%">
      <div class="tabletitle">
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="input-wrap">
              <div class="input-label">
                <span class="mustinput">*</span>姓名
              </div>
              <div class="input-content">
                <el-input v-model="project.name" placeholder="请输入"></el-input>
              </div>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="input-wrap">
              <div class="input-label">年龄(岁)</div>
              <div class="input-content">
                <el-input v-model="project.age" placeholder="请输入年龄" style="width:100%" />
                <!--<el-input v-model="project.age" placeholder="请输入"></el-input>-->
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="input-wrap">
              <div class="input-label">
                <span class="mustinput">*</span>重要程度
              </div>
              <div class="input-content">
                <el-rate v-model="project.importanceDegree" style="margin-top:10px"></el-rate>
              </div>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="input-wrap">
              <div class="input-label">身份</div>
              <div class="input-content">
                <el-input v-model="project.identity" placeholder="请输入"></el-input>
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="input-wrap">
              <div class="input-label">性别</div>
              <div class="input-content">
                <el-radio-group v-model="project.sex" style="margin-top:10px">
                  <el-radio label="0">男</el-radio>
                  <el-radio label="1">女</el-radio>
                  <el-radio label="2">未知</el-radio>
                </el-radio-group>
              </div>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="input-wrap">
              <div class="input-label">婚姻状况</div>
              <div class="input-content">
                <el-select v-model="project.maritalStatus" placeholder="婚姻状况">
                  <el-option
                    v-for="dict in Options"
                    :key="dict.value"
                    :label="dict.text"
                    :value="dict.value"
                  />
                </el-select>
              </div>
            </div>
          </el-col>
          <el-col :span="24">
            <div class="input-wrap">
              <div class="input-label">爱好</div>
              <div class="input-content">
                <el-input v-model="project.interest" placeholder="请输入"></el-input>
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="input-wrap">
              <div class="input-label">职业</div>
              <div class="input-content">
                <el-input v-model="project.profession" placeholder="请输入"></el-input>
              </div>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="input-wrap">
              <div class="input-label">单位</div>
              <div class="input-content">
                <el-input v-model="project.unit" placeholder="请输入"></el-input>
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="input-wrap">
              <div class="input-label">部门</div>
              <div class="input-content">
                <el-input v-model="project.dept" placeholder="请输入"></el-input>
              </div>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="input-wrap">
              <div class="input-label">岗位</div>
              <div class="input-content">
                <el-input v-model="project.post" placeholder="请输入"></el-input>
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="input-wrap">
              <div class="input-label">联系电话</div>
              <div class="input-content">
                <el-input v-model="project.phonenumber" placeholder="请输入"></el-input>
              </div>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="input-wrap">
              <div class="input-label">电子邮箱</div>
              <div class="input-content">
                <el-input v-model="project.email" placeholder="请输入"></el-input>
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <div class="input-wrap">
              <div class="input-label">联系地址</div>
              <div class="input-content">
                <el-input v-model="project.address" placeholder="请输入" type="textarea" :rows="2"></el-input>
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <div class="input-wrap">
              <div class="input-label">备注</div>
              <div class="input-content">
                <el-input v-model="project.remark1" placeholder="请输入" type="textarea" :rows="2"></el-input>
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <div class="input-wrap">
              <div class="input-label">备注2</div>
              <div class="input-content">
                <el-input v-model="project.remark2" placeholder="请输入" type="textarea" :rows="2"></el-input>
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <div class="input-wrap">
              <div class="input-label">备注3</div>
              <div class="input-content">
                <el-input v-model="project.remark3" placeholder="请输入" type="textarea" :rows="2"></el-input>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sumbit" :disabled="canClick">确 定</el-button>
      </span>
    </el-dialog>
    <note-list
      :dialogVisible="note"
      @changedialogVisible="changedialogVisible"
      :title="noteTitle"
      :isProject="isProject"
      :stakeholderId="stakeholderId"
      v-if="note"
    ></note-list>
    <!-- 预览 -->
    <el-dialog :visible.sync="openPreview" width="80%">
      <iframe :src="manualPreviewUrl" frameborder="0" width="100%" height="700px"></iframe>
    </el-dialog>
  </div>
</template>
<script>
import { listStakeholder, infoStakeholder, updateStakeholder, addStakeholder, delStakeholder } from '@/api/projectWorkbench/projectList/projectStakeholder'
import aTitle from '@/components/Title'
import NoteList from '@/components/Note/noteList'
export default {
  name: 'projectStakeholder',
  components: { aTitle, NoteList },
  data () {
    return {
      manualPreviewUrl: null,
      openPreview: false,
      stakeholderId: null,
      isProject: true,
      isSumbit: true,
      noteTitle: '项目干系人便签',
      note: false,
      title: '添加项目干系人',
      loading: true,
      total: 0,
      dialogVisible: false,
      tableData: [],
      project: {
        name: '',
        age: '',
        importanceDegree: 0,
        identity: '',
        sex: '',
        maritalStatus: '',
        interest: '',
        profession: '',
        unit: '',
        dept: '',
        post: '',
        phonenumber: '',
        status: '',
        email: '',
        address: '',
        remark1: '',
        remark2: '',
        remark3: ''
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        name: undefined,
        status: undefined,
        projectId: undefined
      },
      multipleSelection: [],
      multiple: true,
      statusOptions: [
        {
          text: '全部',
          value: undefined
        },
        {
          text: '一星',
          value: '1'
        },
        {
          text: '二星',
          value: '2'
        },
        {
          text: '三星',
          value: '3'
        },
        {
          text: '四星',
          value: '4'
        },
        {
          text: '五星',
          value: '5'
        }
      ],
      Options: [
        {
          text: '未婚',
          value: '0'
        },
        {
          text: '已婚',
          value: '1'
        },
        {
          text: '离异',
          value: '2'
        }
      ],
      isvalid: false,
      canClick: false,
    }
  },
  watch: {
    'project.age' (val) {
      this.project.age = val.match(/\d/ig) ? val.match(/\d/ig).join('') : ''
    }
  },
  created () {
    this.viewList()
    this.queryParams.projectId = this.$route.params.id
    this.isvalid = sessionStorage.getItem('isvalid') === '1' ? true : false;
  },
  mounted(){
    document.title=document.getElementById("projectNameTitle").innerHTML;
  },
  methods: {
    preview () {
      this.openPreview = true;
      this.manualPreviewUrl = `${this.previewUrl}${encodeURIComponent(
        `${this.operationManual}项目主页/业务系统操作说明-项目主页-项目干系人.xlsx`
      )}&name=${encodeURIComponent("业务系统操作说明-项目主页-项目干系人.xlsx")}`;
    },
    viewList () {
      //查询业务列表
      this.loading = true
      listStakeholder(this.queryParams).then(res => {
        if (res.code === 200) {
          this.total = res.total
          this.tableData = res.rows
          this.loading = false
        }
      })
    },
    reset () {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 20;
      this.queryParams.name = undefined;
      this.queryParams.status = '';
      this.queryParams.importanceDegree = undefined;
      this.viewList()
    },
    handleSelectionChange (val) {
      this.multipleSelection = val;
      this.multiple = !val.length
    },
    handleEdit (index, row) {
      this.title = '编辑项目干系人'
      infoStakeholder(row.guid).then(res => {
        if (res.code === 200) {
          this.project = res.data
          this.isSumbit = false;
          this.dialogVisible = true
        }
      })
    },
    sumbit () {
      if (this.project.name === null || this.project.name.trim() === "") {
        this.msgError('姓名不能为空')
        return false
      }

      if (this.project.importanceDegree == 0) {
        this.msgError('重要程度不能为空')
        return false
      }
      if (this.project.phonenumber) {
        if (!(/^1[3456789]\d{9}$/.test(this.project.phonenumber))) {
          this.msgError('请输入正确手机号');
          return false
        }
      }
      if (this.project.email) {
        if (!(/^([a-zA-Z\d])(\w|\-)+@[a-zA-Z\d]+\.[a-zA-Z]{2,4}$/.test(this.project.email))) {
          this.msgError('请输入正确邮箱');
          return false
        }
      }
      this.canClick = true;
      let { id, ...stakeholder } = this.project
      stakeholder.projectId = this.$route.params.id
      if (this.isSumbit) {
        addStakeholder(stakeholder).then(res => {
          if (res.code === 200) {
            this.msgSuccess("新增成功");
            this.viewList()
            this.dialogVisible = false
          }
          this.canClick = false
        })
      } else {
        updateStakeholder(stakeholder).then(res => {
          if (res.code === 200) {
            this.msgSuccess("修改成功");
            this.viewList()
            this.dialogVisible = false
          }
        })
      }
    },
    handleView (index, row) {
      this.stakeholderId = row.guid
      this.note = true
    },
    changedialogVisible (val) {
      this.note = val
    },
    handleAdd () {
      this.title = '添加项目干系人'
      Object.keys(this.project).forEach(key => {
        if (key === "importanceDegree") {
          this.project[key] = 0
        }
        else {
          this.project[key] = ''
        }
      })

      this.isSumbit = true;
      this.dialogVisible = true
      this.canClick = false
    },
    handleDelete () {
      let delId = this.multipleSelection.map(item => item.guid)
      delStakeholder(delId).then(res => {
        this.msgSuccess("删除成功");
        this.viewList()
      })
    },
    tableSortChange (column) {
      this.queryParams.pageNum = 1;
      if (column.order === "descending") {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "desc";
      } else {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "asc";
      }
      this.viewList();
    }
  }
}
</script>
<style lang="scss" scoped>
.tabletitle {
  margin: 0 20px;
  .input-wrap {
    padding: 8px;
    display: flex;

    .input-label {
      min-width: 1em;
      max-width: 8em;
      color: rgba(0, 0, 0, 0.65);
      margin-right: 8px;
      display: flex;
      margin-top: 9px;
      flex: 0 0 120px;
    }

    .input-content {
      -webkit-box-flex: 1;
      flex-grow: 1;
      .el-select {
        width: 100%;
      }
    }
  }
}
.tablebox {
  height: 100%;
}
/deep/.el-table .cell {
  font-family: PingFangSC-Regular;
  color: rgba(0, 0, 0, 0.85);
  letter-spacing: 0;
  text-align: center;
  a {
    font-family: PingFangSC-Regular;
    font-size: 14px;
    color: #1890ff;
    letter-spacing: 0;
    text-align: right;
    line-height: 14px;
    margin: 0 5px;
  }
}
</style>